﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity.Migrations;
using _1._6._2015_EntityFramework.Data.Models;

namespace _1._6._2015_EntityFramework.Data
{
    public static class Seeder
    {
        public static void Seed(OrdersDbContext db)
        {
            db.Users.AddOrUpdate(u => new { u.FirstName, u.LastName },
                new User
                {
                    FirstName = "Nick",
                    LastName = "Brittain"
                },
                new User
                {
                    FirstName = "Susan",
                    LastName = "Anderson"
                });

            db.Items.AddOrUpdate(u => u.Description,
                new Item
                {
                    Description = "Peanut Butter",
                    Price = 3.99m
                },
                new Item
                {
                    Description = "Refrigerator",
                    Price = 1299.99m
                });

            db.SaveChanges();

            if (!db.Orders.Any())
            {
                User nick = db.Users.FirstOrDefault(u => u.FirstName == "Nick" && u.LastName == "Brittain");
                User susan = db.Users.FirstOrDefault(u => u.FirstName == "Susan" && u.LastName == "Anderson");

                Order o = new Order
                {
                    OrderDate = DateTime.Now,
                    Total = 45.00m,
                    User = nick
                };

                db.Orders.Add(o);

                OrderItem oi = new OrderItem
                {
                    Order = o,
                    Price = 65.94m,
                    Item = db.Items.FirstOrDefault()
                };

                db.OrderItems.Add(oi);

                OrderItem oi2 = new OrderItem
                {
                    Order = o,
                    Price = 546.34m,
                    Item = db.Items.FirstOrDefault(i => i.Description == "Refrigerator")
                };

                db.OrderItems.Add(oi2);

                db.SaveChanges();
            }
        }
    }
}
